#!/bin/bash
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements.  See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership.  The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License.  You may obtain a copy of the License at
#
#   http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied.  See the License for the
# specific language governing permissions and limitations
# under the License.

OS=`awk -F= '/^NAME/{print $2}' /etc/os-release`
REQUIRED_PACKAGES=(cloud-init cloud-guest-utils conntrack apt-transport-https ca-certificates curl gnupg gnupg-agent \
                    software-properties-common gnupg lsb-release python3-json-pointer python3-jsonschema cloud-init containerd.io)
declare -a MISSING_PACKAGES
if [[ $OS == *"Ubuntu"* || $OS == *"Debian"* ]]; then
  for package in ${REQUIRED_PACKAGES[@]}; do
    dpkg -s $package >/dev/null 2>&1
    if [ $? -eq 1 ]; then
      MISSING_PACKAGES+="$package"
    fi
  done
else
  for package in ${REQUIRED_PACKAGES[@]}; do
    rpm -qa | grep $package >/dev/null 2>&1
    if [ $? -eq 1 ]; then
      MISSING_PACKAGES[${#MISSING_PACKAGES[@]}]=$package
    fi
  done
fi

echo ${#MISSING_PACKAGES[@]}
if (( ${#MISSING_PACKAGES[@]} )); then
  echo "Following packages are missing in the node template: ${MISSING_PACKAGES[@]}"
  exit 1
else
  echo 0
fi
